package com.qipay.sp.excutor.yitong;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class YtWxNativeExcutor extends AbstractExcutor {

    private final static String API_URL="api_url";
    private final static String SHOP_ID="shop_id";


    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_WAP_PAY;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("user_id", order.getCpId());
        apiParams.put("money", new BigDecimal(order.getAmount()).divide(new BigDecimal("100")).doubleValue()+"");
        apiParams.put("type","wechat");
        apiParams.put("shop_no", order.getTradeNo());
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Object apiUrl = getVal(spconfig, API_URL);
        Object shopId= getVal(spconfig, SHOP_ID);
        Object key= getVal(spconfig, "key");
        Object notifyUrl= getVal(spconfig, "notify_url");
        Object returnUrl= getVal(spconfig, "return_url");
        params.put("shop_id",shopId);
        params.put("notify_url",notifyUrl);
        params.put("return_url",returnUrl);

        String signStr = shopId.toString()+params.get("user_id").toString()+params.get("money").toString()+params.get("type").toString()+key.toString();
        String sign = MD5Util.sign(signStr,"UTF-8").toLowerCase();
        params.put("sign",sign);

        String result = doJsonPost(apiUrl.toString(),JSONObject.toJSONString(params));
//        {"order_no":"HD77QKF1539328899860","user_id":"1000003","money":"10.00","type":"wechat",
// "pay_url":"wxp://f2f1TULLpmCd9ja3y9mHQsazLhDEfI1x4a-P","sign":"55a565b1c57e95cd5f5dee30472b37ac","shop_no":"2018101215212610000",
// "qrcode_url":"https://www.yn63.com/apiShop?orderId=b4a2d3e1e2315b7ab317f1dd22e1b628"}
        if (StringUtils.isBlank(result)) {
            throw new Exception("请求亿通的微信接口失败");
        }

        Map<String, Object> responseMap = JSONObject.parseObject(result);
        Map<String, Object> resultMap = new HashMap<>();

        if (responseMap == null || !responseMap.containsKey("pay_url") ) {
            logger.error("请求亿通的微信接口失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("message", "请截图给上游客服：亿通接口请求失败");
            return resultMap;
        }

        resultMap.put("result_code", "0");
        resultMap.put("code_url", responseMap.get("pay_url"));
        resultMap.put("code_img_url", responseMap.get("qrcode_url"));


        return resultMap;
    }
}
